package com.xt.purchasingsystem.service.impl.productionManagementServiceImpl;

import com.xt.purchasingsystem.dto.DFileDto;
import com.xt.purchasingsystem.dto.MDesignProcedureDto;
import com.xt.purchasingsystem.mapper.DFileMapper;
import com.xt.purchasingsystem.mapper.MDesignProcedureDetailsMapper;
import com.xt.purchasingsystem.mapper.MDesignProcedureMapper;
import com.xt.purchasingsystem.model.DFile;
import com.xt.purchasingsystem.model.MDesignProcedureDetails;
import com.xt.purchasingsystem.service.productionManagementService.ProductProductionProcessDesignService;
import com.xt.purchasingsystem.utils.PageBean;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SerialNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 生产管理--产品生产工序设计的业务实现类
 * Author: 老姚
 * Date:  2020/3/16 10:44
 */
@Service
@Transactional
public class ProductProductionProcessDesignServiceImpl implements ProductProductionProcessDesignService {
    @Autowired
    private DFileMapper dFileMapper;
    @Autowired
    private MDesignProcedureMapper mDesignProcedureMapper;
    @Autowired
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;



    @Override
    public Result queryDFileByMaterialCompositionCompleted(DFileDto dFileDto) {
        int rowCount = dFileMapper.rowCountByMaterialCompositionCompleted(dFileDto);
        PageBean pageBean=new PageBean(dFileDto.getPageSize(),dFileDto.getCurrNo(),rowCount);
        dFileDto.setCurrNo(pageBean.getStart());
        dFileDto.setPageSize(pageBean.getEnd());
        List<DFile> dFiles = dFileMapper.queryDFileByMaterialCompositionCompleted(dFileDto);
        pageBean.setPageList(dFiles);
        return ResultFactory.buildSuccessResult("查询成功",pageBean);
    }

    @Override
    public Result addProductionDesignSheet(MDesignProcedureDto mDesignProcedureDto) {
        dFileMapper.designDFileProcedureTagById(mDesignProcedureDto.getMdesignProcedure().getProductId());
        mDesignProcedureDto.getMdesignProcedure().setDesignId(SerialNumberUtil.processDesignSerNumber());
        mDesignProcedureMapper.insert(mDesignProcedureDto.getMdesignProcedure());
        Integer id =mDesignProcedureDto.getMdesignProcedure().getId();
        for (int i = 0; i < mDesignProcedureDto.getMdesignProcedureDetails().size(); i++) {
            mDesignProcedureDto.getMdesignProcedureDetails().get(i).setParentId(id);
            mDesignProcedureDto.getMdesignProcedureDetails().get(i).setDetailsNumber(i+1);
            mDesignProcedureDetailsMapper.insert( mDesignProcedureDto.getMdesignProcedureDetails().get(i));
        }
        return  ResultFactory.buildSuccessResult("提交成功,需要审核");
    }

    @Override
    public Result queryAllunreviewedMDesignProcedureByPaging(MDesignProcedureDto mDesignProcedureDto) {
        int rowCount = mDesignProcedureMapper.rowCountAllunreviewedMDesignProcedure();
        PageBean pageBean=new PageBean(mDesignProcedureDto.getPageSize(),mDesignProcedureDto.getCurrNo(),rowCount);
        mDesignProcedureDto.setCurrNo(pageBean.getStart());
        mDesignProcedureDto.setPageSize(pageBean.getEnd());
        List<MDesignProcedureDto> mDesignProcedureDtos = mDesignProcedureMapper.queryAllunreviewedMDesignProcedureByPaging(mDesignProcedureDto);
        pageBean.setPageList(mDesignProcedureDtos);
        return ResultFactory.buildSuccessResult("查询成功",pageBean);
    }

    @Override
    public Result reviewProductionDesignSheet(MDesignProcedureDto mDesignProcedureDto) {
        mDesignProcedureMapper.updateByPrimaryKeySelective(mDesignProcedureDto.getMdesignProcedure());
        for (MDesignProcedureDetails mdesignProcedureDetail : mDesignProcedureDto.getMdesignProcedureDetails()) {
            mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(mdesignProcedureDetail);
        }
        return ResultFactory.buildSuccessResult("审核成功");
    }
}
